const util = require("../../../../utils/util.js")
const api = require("../../../../wxapi/config.js")
const tagMap = {
  '剧集': 1, '电视剧': 2, '网剧': 3, '电影': 4, '网络大电影': 5, '院线电影': 6
};
Page({
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '影视行业大数据',
      path: '/pages/index/RankingList/netplatform/netplatform'
    }
  },

	/**
	 * 页面的初始数据
	 */
	data: { 
    summaryList: [],
    minDate: new Date(2016, 12, 1).getTime(),
    maxDate: new Date().getTime(),
    currentDate: new Date().getTime(),
    currentDateStr: util.formatDate(new Date()),
    tabletitle: '节目',
    previous: false,
    next: true,
    isShowPicker: false,
    isFilm: false,
    selectTitle1: '全部',
    selectTitle2: '电视剧',
    selectTitle3: '网剧',
    selectTitle4: '剧集',
    tableHead1: '名称',
    tableHead2: '播放量',
    tableHead3: '热度',
    tableHead4: '观看指数',
    tag: 0,
    tagPara: 1,
		isloading: true,
		skeleton: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    active: 0,

    tabTypeActive: '全部',
    seriesActiveOpt: [{ name: '全部' }, { name: '电视剧' }, { name: '网剧' }],
    filmActiveOpt: [{ name: '全部' }, { name: '院线电影' }, { name: '网络大电影' }],
    isShowTypes: false,

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
    let dateInit = new Date()
    this.setData({
      maxDate: dateInit.getTime() - 24 * 60 * 60 * 1000,
      currentDate: dateInit.getTime() - 24 * 60 * 60 * 1000,
      currentDateStr: util.formatDate(new Date(dateInit.getFullYear(), dateInit.getMonth(), dateInit.getDate() - 1))
    })
    this.getData(this.data.currentDateStr, this.data.tagPara)
	},

  getData (date, tag) {
		this.setData({
			isloading: true
		})
    api.getSummaryList({rq: date, tag: tag}).then((res) => {
      if (res.data.length !== 0) {
        this.setData({
          summaryList: res.data,
					isloading: false
        })
      } else {
        this.setData({
          summaryList: [],
					isloading: false
        })
      }
    })
  },

  // 切换剧集/电影
  onChangeNav(event) {
    if (event.detail.index === 0) {
      this.setData({
        tag: 0,
        tagPara: 1,
        selectTitle1: '全部',
        tabTypeActive: '全部',
        selectTitle2: '电视剧',
        selectTitle3: '网剧',
        selectTitle4: '剧集',
        tableHead1: '名称',
        tableHead2: '播放量',
        tableHead3: '热度',
        tableHead4: '观看指数',
        active: 0
      })
    }
    if (event.detail.index === 1) {
      this.setData({
        tag: 1,
        tagPara: 4,
        selectTitle1: '全部',
        tabTypeActive: '全部',
        selectTitle2: '院线电影',
        selectTitle3: '网络大电影',
        selectTitle4: '电影',
        tableHead1: '名称',
        tableHead2: '日播放量',
        tableHead3: '总播放量',
        tableHead4: '',
        active: 0
      })
    }

    this.getData(this.data.currentDateStr, this.data.tagPara)
  },

  // 标签切换
  onChangeBomtNav(event) {
    console.log(event)
    this.setData({
      tagPara: event.detail.name === '全部'
         ? tagMap[this.data.selectTitle4] 
         : tagMap[event.detail.title],
      tabTypeActive: event.detail.name
    })
    this.getData(this.data.currentDateStr, this.data.tagPara)
  },

  // 前一天
  changeprev() {
    if (!this.data.previous) {
      let time = util.computedDate(this.data.minDate, this.data.currentDate, this.data.maxDate, 0)
      this.data.currentDate -= (86400000 + time)
      let date = new Date(this.data.currentDate)
      this.setData({
        currentDate: this.data.currentDate
      })
      if (this.data.currentDate > this.data.minDate) {
        this.setData({
          currentDateStr: util.formatDate(date),
          previous: false,
          next: false
        })
      } else {
        this.setData({
          currentDateStr: util.formatDate(new Date(this.data.minDate)),
          previous: true
        })
      }
      this.getData(this.data.currentDateStr, this.data.tagPara)
    }
  },

  // 后一天
  changenext() {
    if (!this.data.next) {
      let time = util.computedDate(this.data.minDate, this.data.currentDate, this.data.maxDate, 2)
      this.data.currentDate += (86400000 + time)
      let date = new Date(this.data.currentDate)
      this.setData({
        currentDate: this.data.currentDate
      })
      if (this.data.currentDate < this.data.maxDate) {
        this.setData({
          currentDateStr: util.formatDate(date),
          previous: false,
          next: this.data.currentDate === this.data.maxDate ? true : false
        })
      } else {
        this.setData({
          currentDateStr: util.formatDate(new Date(this.data.maxDate)),
          next: true
        })
      }
      this.getData(this.data.currentDateStr, this.data.tagPara)
    }
  },

  // 选择日期后点击【确认】的响应事件
  changeDate(event) {
    const date = new Date(event.detail);
    this.setData({
      isShowPicker: false,
      currentDate: util.computedDate(this.data.minDate, event.detail, this.data.maxDate, 1),
      currentDateStr: util.formatDate(date)
    })
    this.setData({
      previous: event.detail === this.data.minDate,
      next: this.data.currentDate > this.data.minDate && this.data.currentDate === this.data.maxDate
    })
    this.getData(this.data.currentDateStr, this.data.tagPara)
  },

  // 显示日期选择组件
  showDatetimeOpt() {
    this.setData({
      isShowPicker: true
    })
  },
  // 取消日期选择组件
  hideDatetimeOpt() {
    this.setData({
      isShowPicker: false
    })
  },
  // 显示选择【类型】组件
  isShowTypesCompon() {
    this.setData({
      isShowTypes: true
    })
  },
  // 隐藏选择【类型】组件
  hideTypesCompon() {
    this.setData({
      isShowTypes: false
    })
  },
  onChangeBomtSel(event) {
    console.log(event)
    this.setData({
      tagPara: event.detail.name !== '全部'
        ? tagMap[event.detail.name]
        : this.data.tag === 0
          ? 1
          : 4,
      tabTypeActive: event.detail.name,
      isShowTypes: false
    })
    this.getData(this.data.currentDateStr, this.data.tagPara)
  }
 
})